<?php

namespace Wap\Controller;

use Common\Controller\CommonController;

class WapController extends CommonController {

    protected $member;

    public function _initialize() {
        // 非ajax操作的时候,检查微信用户session
        unset( $_SESSION['member']);
        $_SESSION['member']['openid'] ="o6F-qju1_MFXVYHn9HZSy6hl6Dus";
        if (!IS_AJAX) {
            if (!(isset($_SESSION['member']) && $_SESSION['member'])) {
                redirect('http://' . $_SERVER['HTTP_HOST'] . '/weixin.php'); // 跳转到该链接去获取微信信息
            } else {
                $where['wechat'] = $_SESSION['member']['openid'];
                if (M('members')->where($where)->count()) {
                    M('members')->where($where)->save($_SESSION['member']); // 更新用户信息
                } else {
                    $data['nickname'] = $_SESSION['member']['nickname']==null?"未获取到":$_SESSION['member']['nickname'];
                    $data['wechat'] = $_SESSION['member']['openid'];
                    $data['headimgurl'] = $_SESSION['member']['headimgurl'];
                    $data['add_time'] = NOW_TIME;
                    $data['card']=uniqid();
                  $result =  M('members')->add($data); // 添加用户信息
                }
            }
        }
        $m = M("members");
        $where["wechat"] = array("eq", $_SESSION['member']['openid']);
        $this->member = $m->field(true)->where($where)->find();
    }

    /**
     * 判断是否收藏
     * @param int $tid  收藏类型的id
     * @param int $type 收藏类型
     * @return bool
     */
    protected function _is_collected($tid, $type) {
        $collect_model = M('collect');
        $where = array(
            'type' => $type,
            'mid'  => $this->member['id'],
            'tid'  => $tid,
        );
        return (boolean)$collect_model->where($where)->count();
    }
}
